package com.kawa.kw_customer_server;

import android.app.Activity;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.m7.imkfsdk.KfStartHelper;
import com.moor.imkf.requesturl.RequestUrl;

import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/** KwCustomerServerPlugin */
public class KwCustomerServerPlugin implements FlutterPlugin, MethodCallHandler , ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private Activity mActivity;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "kw_customer_server");
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("openKf")){
      Map<String,String> params = (Map<String, String>) call.arguments;
      openKf(params,result);
    }else{
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  private void openKf(Map<String,String> params,Result result){
    String appId;
    String mobile;
    String userId;
    if (params.get("appId") == null){
      Toast.makeText(mActivity,"客服id为空",Toast.LENGTH_SHORT).show();
      result.notImplemented();
      return;
    }
    appId = params.get("appId");
    if (params.get("mobile") == null){
      Toast.makeText(mActivity,"客服电话为空",Toast.LENGTH_SHORT).show();
      result.notImplemented();
      return;
    }
    mobile = params.get("mobile");
    if (params.get("userId") == null){
      Toast.makeText(mActivity,"userId",Toast.LENGTH_SHORT).show();
      result.notImplemented();
      return;
    }
    userId = params.get("userId");
    final KfStartHelper helper = KfStartHelper.getInstance();
    helper.setHelper(mActivity);
    RequestUrl.setRequestBasic(RequestUrl.Tencent_REQUEST);
    helper.initSdkChat(appId, mobile, userId);
    result.success(true);
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    mActivity = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {

  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

  }

  @Override
  public void onDetachedFromActivity() {

  }
}
